/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaServiceVersion;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class ServiceVersionDao {

	public static List getServiceVersionByServiceId(String serId)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String sql = "from SoaServiceVersion s where s.serviceId = ? order by s.publishTime asc";
			List ret = DaoManager.getDao().find(sql, serId);

			tx.commit();

			return ret;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceVersionDao.class);
			logger.error("ServiceVersionDao.getServiceVersionByServiceId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceVersionByServiceId() error", e);
		} finally {
			tx.end();
		}

	}

	public static List getVersionListByServiceId(String serId)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String sql = "select s.version from SoaServiceVersion s where s.serviceId = ? order by s.publishTime asc";
			List list = DaoManager.getDao().find(sql, serId);

			tx.commit();

			return list;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceVersionDao.class);
			logger.error("ServiceVersionDao.getServiceVersionByServiceId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceVersionByServiceId() error", e);
		} finally {
			tx.end();
		}

	}

	public static SoaServiceVersion getServiceVersionById(String id)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			SoaServiceVersion v = (SoaServiceVersion) DaoManager.getDao().get(
					SoaServiceVersion.class, id);

			tx.commit();

			return v;

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceVersionDao.class);
			logger.error("ServiceVersionDao.getServiceVersionByServiceId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getServiceVersionByServiceId() error", e);
		} finally {
			tx.end();
		}

	}

	public static String createVersion(SoaServiceVersion v)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			String hql = "from SoaServiceVersion s where s.serviceCode = ? and s.version = ?";
			List l = DaoManager.getDao().find(hql,
					new Object[] { v.getServiceCode(), v.getVersion() });
			if (l.size() > 0) {
				throw new BifangException(BifangErrorCode.VERSION_EXIST_ERROR,
						"this version [" + v.getVersion() + " exists");
			}
			DaoManager.getDao().save(v);
			tx.commit();
			return v.getId();

		} catch (BifangException e) {
			throw e;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceVersionDao.class);
			logger.error("ServiceVersionDao.createVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createVersion() error", e);
		} finally {
			tx.end();
		}
	}

	public static void modifyVersion(SoaServiceVersion v)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().merge(v);
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceVersionDao.class);
			logger.error("ServiceVersionDao.modifyVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"modifyVersion() error", e);
		} finally {
			tx.end();
		}
	}

	public static void removeVersion(SoaServiceVersion v)
			throws BifangException {
		Tx tx = null;

		try {
			tx = TxManager.start();
			DaoManager.getDao().delete(v);
			tx.commit();

		} catch (Throwable e) {
			Log logger = LogFactory.getLog(ServiceVersionDao.class);
			logger.error("ServiceVersionDao.removeVersion()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeVersion() error", e);
		} finally {
			tx.end();
		}
	}

}
